package controller

import (
	"appService/src/application"
	"appService/src/application/app/appModels"
	"github.com/gogf/gf/net/ghttp"
	"xorm.io/builder"
	"xorm.io/xorm"
)

//Dictionary 字典
type Dictionary struct {
	application.BaseController
}

func (c *Dictionary) getModel() appModels.IModel {
	return new(appModels.Dictionary)
}

//Get 字典
func (this *Dictionary) Get(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	req.GetExDiy("key", func(db *xorm.Session, condition string) error {
		db.Where(builder.Eq{"only_key": condition, "type": 1, "enable": 1})
		return nil
	})
}

//Add 字典
func (this *Dictionary) Add(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.AddEx(func(db *xorm.Session, guid string) error {
		dict.Type = 1
		return dict.Init(db, guid, req.LoginUser)
	}, func(db *xorm.Session) error {
		return dict.AddValid()
	}, func(db *xorm.Session) error {
		return dict.AddWhere(db, req.LoginUser)
	})
}

//Delete 字典
func (this *Dictionary) Delete(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.DeleteEx(func(db *xorm.Session, ids ...string) error {
		dict.Type = 1
		return dict.DeleteWhere(db, req.LoginUser, ids...)
	})
}

//Edit 字典
func (this *Dictionary) Edit(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.EditEx(func(db *xorm.Session) error {
		return dict.EditValid()
	}, func(db *xorm.Session) error {
		dict.Type = 1
		return dict.EditWhere(db, req.LoginUser)
	})
}

//List 字典
func (this *Dictionary) List(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.ListEx(func() (slice interface{}, table interface{}) {
		return dict.Slice(), req.Model.Model()
	}, func(db *xorm.Session) error {
		dict.Type = 1
		return dict.ListWhere(db, req.LoginUser)
	})
}

//ListPaginate 字典
func (this *Dictionary) ListPaginate(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.ListPaginateEx(func() (slice interface{}, table interface{}) {
		return dict.Slice(), req.Model.Model()
	}, func(db *xorm.Session) error {
		dict.Type = 1
		return dict.ListWhere(db, req.LoginUser)
	}, func(db *xorm.Session) error {
		dict.Type = 1
		return dict.PaginateWhere(db, req.LoginUser)
	})
}

//GetSys 系统设置
func (this *Dictionary) GetSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	req.GetExDiy("key", func(db *xorm.Session, condition string) error {
		db.Where(builder.Eq{"type": 2, "only_key": condition, "enable": 1})
		return nil
	})
}

//AddSys 系统设置
func (this *Dictionary) AddSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.AddEx(func(db *xorm.Session, guid string) error {
		dict.Type = 2
		return dict.Init(db, guid, req.LoginUser)
	}, func(db *xorm.Session) error {
		return dict.AddValid()
	}, func(db *xorm.Session) error {
		return dict.AddWhere(db, req.LoginUser)
	})
}

//DeleteSys 系统设置
func (this *Dictionary) DeleteSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.DeleteEx(func(db *xorm.Session, ids ...string) error {
		dict.Type = 2
		return dict.DeleteWhere(db, req.LoginUser, ids...)
	})
}

//EditSys 系统设置
func (this *Dictionary) EditSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.EditEx(func(db *xorm.Session) error {
		return dict.EditValid()
	}, func(db *xorm.Session) error {
		dict.Type = 2
		return dict.EditWhere(db, req.LoginUser)
	})
}

//ListSys 系统设置
func (this *Dictionary) ListSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.ListEx(func() (slice interface{}, table interface{}) {
		return dict.Slice(), req.Model.Model()
	}, func(db *xorm.Session) error {
		dict.Type = 2
		return dict.ListWhere(db, req.LoginUser)
	})
}

//ListPaginateSys 系统设置
func (this *Dictionary) ListPaginateSys(r *ghttp.Request) {
	req := this.NewRequest(r, this.getModel())
	dict := req.Model.(*appModels.Dictionary)
	req.ListPaginateEx(func() (slice interface{}, table interface{}) {
		return dict.Slice(), req.Model.Model()
	}, func(db *xorm.Session) error {
		dict.Type = 2
		return dict.ListWhere(db, req.LoginUser)
	}, func(db *xorm.Session) error {
		dict.Type = 2
		return dict.PaginateWhere(db, req.LoginUser)
	})
}
